package com.ydlclass.serialize.impl;

import com.alibaba.fastjson2.JSON;
import com.ydlclass.serialize.Serializer;
import lombok.extern.slf4j.Slf4j;

import java.io.*;

/**
 *
 */
@Slf4j
public class JsonSerializer implements Serializer {
    @Override
    public byte[] serialize(Object object) {
        if(object == null){
            return null;
        }

        byte[] result = JSON.toJSONBytes(object);
        if(log.isDebugEnabled()){
            log.debug("对象{}已经完成了序列化,序列化长度为{}",object,result.length);
        }
        return result;
    }

    @Override
    public <T> T deserialize(byte[] bytes, Class<T> clazz) {
        if (bytes == null || clazz == null){
            return null;
        }
        T t = JSON.parseObject(bytes,clazz);
        if(log.isDebugEnabled()){
            log.debug("对象{}已经完成了反序列化操作",clazz);
        }
        return t;
    }
}
